//
//  CREditInformationController.m
//  CRChatRoom
//
//  Created by CuiFengshuai on 15/11/25.
//  Copyright © 2015年 SecondGroup. All rights reserved.
//

#import "CREditInformationController.h"
#import "CRFrinedDetailView.h"
#import "CRUserDetailModel.h"
#import "CRLTVIew.h"
#import "CRTool.h"
#import "MBProgressHUD+MJ.h"
#import "CRTextView.h"

@class LTView;

@interface CREditInformationController () <UINavigationControllerDelegate, UIImagePickerControllerDelegate, UITextFieldDelegate, UIScrollViewDelegate>

@property (nonatomic, strong) CRFrinedDetailView *detailView;

/**
 *  用户信息模型
 */
@property (nonatomic, strong) CRUserDetailModel *detailModel;

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, weak) UITextField *jobtext;

@end

@implementation CREditInformationController

- (void)loadView
{
    [super loadView];
    
    
    self.detailView = [[CRFrinedDetailView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view  = self.detailView;
}

#pragma mark >>>> 系统方法
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UIButton *button = [[UIButton alloc] init];
    button.bounds = CGRectMake(0, 0, 30, 30);
    [button setBackgroundImage:[UIImage imageNamed:@"backNormal"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"baclhigh"] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(didLeftBarButtonItem) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(didLeftBarButtonItem:)];
    
    
    // 界面进来显示信息----通过objectId来判断看自己的信息还是看好友的信息
    //    NSString *ID = [AVUser currentUser].objectId;
    
    [CRTool getUserInfoWithobjectId:[AVUser currentUser].objectId withblock:^(NSDictionary *dict) {
        self.detailModel = [[CRUserDetailModel alloc] init];
        [self.detailModel setValuesForKeysWithDictionary:dict];
        self.detailView.friendDetailModel = self.detailModel;
    }];
    
    
    self.detailModel = [[CRUserDetailModel alloc] init];
    // 头像--给头像添加手势
    self.detailView.iconImageView.userInteractionEnabled = YES;
    [self.detailView.iconImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(photoTap)]];
    
    
    // 保存按钮添加监听事件
    [self.detailView.chatButton addTarget:self action:@selector(clickSaveButton) forControlEvents:UIControlEventTouchUpInside];
    
    //监听弹出键盘
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    //监听收回键盘
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
    
}


#pragma mark >>>> 监听键盘事件

/**
 *  键盘即将显示
 */
- (void)keyBoardWillShow:(NSNotification *)notification
{
    CGRect rect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat ty = rect.size.height * 0.5;
    CGFloat H = [UIScreen mainScreen].bounds.size.height;
    CGFloat off = CGRectGetMaxY(self.detailView.address.frame);
    
    if (((H - off) - ty) < 150 && ([self.detailView.address.rightField isFirstResponder] || [self.detailView.hometownLable.rightField isFirstResponder] || [self.detailView.hobbyLable.rightField isFirstResponder])) {
        [UIView animateWithDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue] animations:^{
            self.view.transform = CGAffineTransformMakeTranslation(0, -ty);
        }];
    }
}
/**
 *   键盘即将收回
 */
- (void)keyBoardWillHide:(NSNotification *)notification
{
    [UIView animateWithDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue] animations:^{
        self.view.transform = CGAffineTransformIdentity;
    }];
}



// 保存数据到的服务器
- (void)clickSaveButton
{
    [self getUserinfo];
    
    if (!self.detailModel.iconImage && !self.detailView.iconImageView.image) {
        
        [MBProgressHUD showError:@"一定要选择头像才能上传"];
    }
    
    [CRTool saveUserInfoWithUserDetailModel:self.detailModel];
}

// 点击头像触发的方法-=-打开相册
- (void)photoTap
{
    // 模态图片选择控制器
    UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
    // 设置资源来源类型
    [ipc setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    
    // 设置为可编辑状态
    ipc.allowsEditing = YES;
    
    ipc.delegate = self;
    [self presentViewController:ipc animated:NO completion:nil];
    
}

#pragma mark - 图片选择控制器的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    // 2. 取得图片
    UIImage *imageNew = info[UIImagePickerControllerEditedImage];
    
    [self.detailView.iconImageView setImage:imageNew];
    
    // 给模型赋值
    self.detailModel.iconImage = imageNew;
    
    // 1. 销毁picker控制器
    [picker dismissViewControllerAnimated:NO completion:nil];
    
}



- (void)getUserinfo;
{
    // 个性签名
    self.detailModel.signature = self.detailView.signatureview.text;
    // 名称
    // 备注
    self.detailModel.remarkName = self.detailView.nicknameLable.rightField.text;
    // 性别
    self.detailModel.gender = self.detailView.gengerLable.rightField.text;
    // 年龄
    self.detailModel.age = self.detailView.ageLable.rightField.text;
    // 生日
    self.detailModel.brithday = self.detailView.birthdayLable.rightField.text;
    // 职业
    self.detailModel.job = self.detailView.jobLable.rightField.text;
    self.jobtext = self.detailView.jobLable.rightField;
    // 住址
    self.detailModel.address = self.detailView.address.rightField.text;
    // 家乡
    self.detailModel.hometown = self.detailView.hometownLable.rightField.text;
    // 兴趣爱好
    self.detailModel.hobby = self.detailView.hobbyLable.rightField.text;
    
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

#pragma mark - 左上角返回按钮
- (void)didLeftBarButtonItem
{
    [self.navigationController popViewControllerAnimated:YES];
}
@end








